import { Component, OnInit, ViewChild } from '@angular/core';
import { PersonalService } from '../../services/project/personal/personal.service';
import { NzMessageService } from 'ng-zorro-antd';
import { Router } from '@angular/router';
declare var CryptoJS: any;
import { FILEURL } from './../../config';

@Component({
  selector: 'app-password',
  templateUrl: './password.component.html',
  styleUrls: ['./password.component.scss']
})
export class PasswordComponent implements OnInit {

  @ViewChild('oldPassWord', { static: false }) oldPassWordComponent;
  @ViewChild('newPassWord', { static: false }) newPassWordComponent;
  @ViewChild('confirmPassWord', { static: false }) confirmPassWordComponent;

  constructor(
    private personalService: PersonalService,
    private message: NzMessageService,
    private router: Router
  ) { }

  public oldPassword = '';
  public newPassword = '';
  public newPasswordConfirm = '';
  public oldPasswordError: any;
  public oldPasswordMessage = '';
  public newPasswordError: any;
  public newPasswordMessage = '';
  public newPasswordConfirmError: any;
  public newPasswordConfirmMessge = '';
  public oldpasswordVisible = false;
  public newpasswordVisible = false;
  public passwordVisible = false;

  //检查密码
  public CheckOldValue() {
    //原密码
    if (this.oldPassword.length == 0) {
      this.oldPasswordError = '';
      return;
    }
    if (this.oldPassword.length >= 50 || this.oldPassword.length < 6) {
      this.oldPasswordError = true;
      this.oldPasswordMessage = '密码长度错误,仅支持6~50位字母/数字/符号组合';
      return;
    }
    this.oldPasswordError = false;
    this.oldPasswordMessage = '';
  }

  public CheckNewValue() {
    //新密码
    if (this.newPassword.length == 0) {
      this.newPasswordError = '';
      return;
    }
    if (this.newPassword.length >= 50 || this.newPassword.length < 6) {
      this.newPasswordError = true;
      this.newPasswordMessage = '密码长度错误,仅支持6~50位字母/数字/符号组合';
      return;
    }
    this.newPasswordError = false;
    this.newPasswordMessage = '';
  }

  public CheckConfirmValue() {
    //确认密码
    if (this.newPasswordConfirm.length == 0) {
      this.newPasswordConfirmError = '';
      return;
    }
    if (this.newPassword.length == 0) {
      this.newPasswordError = true;
      this.newPasswordMessage = '请先输入新密码';
      return;
    }
    if (this.newPassword !== this.newPasswordConfirm) {
      this.newPasswordConfirmError = true;
      this.newPasswordConfirmMessge = '确认密码与新密码不一致';
      return;
    }
    this.newPasswordConfirmError = false;
    this.newPasswordConfirmMessge = '';
  }

  //检查未填项
  public CheckEmpty(): boolean {
    if (this.oldPassword.length == 0) {
      this.oldPasswordMessage = '请输入原密码';
      this.oldPassWordComponent.nativeElement.focus();
      this.oldPasswordError = true;
      return true;
    } else if (this.newPassword.length == 0) {
      this.newPasswordMessage = '请输入新密码';
      this.newPassWordComponent.nativeElement.focus();
      this.newPasswordError = true;
      return true;
    } else if (this.newPasswordConfirm.length == 0) {
      this.newPasswordConfirmMessge = '请输入确认密码';
      this.confirmPassWordComponent.nativeElement.focus();
      this.newPasswordConfirmError = true;
      return true;
    }
  }

  /**
   * 保存
   */
  public async preservation() {
    if (this.CheckEmpty()) { return; }
    if (this.oldPasswordError !== false || this.newPasswordError !== false || this.newPasswordConfirmError !== false) {
      this.message.create('error', '请正确填写后提交');
      return;
    }
    const json = {
      currentPassword: CryptoJS.MD5(this.oldPassword).toString().toUpperCase(),
      newPassword: CryptoJS.MD5(this.newPassword).toString().toUpperCase(),
      newPasswordConfirm: CryptoJS.MD5(this.newPassword).toString().toUpperCase(),
    }
    const result = await this.personalService.editPassword(json);
    if (result.error) {
      this.oldPasswordMessage = '密码错误';
      this.oldPasswordError = true;
      this.oldPassWordComponent.nativeElement.focus();
    } else {
      this.reset();
      this.message.success('密码修改成功，请重新登录！');
      this.router.navigate([`login`]);
    }
  }

  /**
   * 返回
   */
  public comeBack() {
    // this.router.navigate(['outer/dashboard']);
    history.go(-1);
  }

  /**
   * 重置
   */
  public async reset() {
    this.oldPassword = '';
    this.newPassword = '';
    this.newPasswordConfirm = '';
    this.oldPasswordError = '';
    this.oldPasswordMessage = '';
    this.newPasswordError = '';
    this.newPasswordMessage = '';
    this.newPasswordConfirmError = '';
    this.newPasswordConfirmMessge = '';
  }
  // 同步千变数据


  public async async1() {
    const result = await this.personalService.async1();
  }
  public async async2() {
    const result = await this.personalService.async2();
  }
  ngOnInit() { }

}
